﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class RepairBoxListCtrl : BaseListControl, IListControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
        }

        public POSSystem.BLL.View_RepairBoxes BoxList
        {
            get { return (POSSystem.BLL.View_RepairBoxes)SourceObject; }
            set { SourceObject = value; }
        }

        #region IListControl Members

        public void DeleteCheckedItems()
        {
            String oldFilter = Filter;
            try
            {
                String checkedItemID = "";
                for (int i = 0; i < grid.Items.Count; i++)
                {
                    DataGridItem item = grid.Items[i];
                    Control ctrl = item.FindControl("cbSelect");
                    if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                        checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
                }
                if (checkedItemID != "")
                    checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);
                checkedItemID = "(" + checkedItemID + ")";

                if (!String.IsNullOrEmpty(oldFilter))
                    Filter = Filter + " and (" + POSSystem.BLL.RepairBoxes.ColumnNames.ID + " in " + checkedItemID + ")";
                else
                    Filter = POSSystem.BLL.RepairBoxes.ColumnNames.ID + " in " + checkedItemID;

                BoxList.Rewind();
                for (int i = 0; i < BoxList.RowCount; i++)
                {
                    if (!BoxList.IsFree)
                    {
                        throw new ApplicationException(string.Format("Box {0} is Busy. You can not delete a box whose status is busy.", BoxList.RepairBoxName));
                    }

                    BoxList.MoveNext();
                }

                BoxList.Rewind();
                do
                {
                    if(BoxList.RowCount > 0)
                        BoxList.MarkAsDeleted();
                } while (BoxList.MoveNext());

                Filter = oldFilter;

                RepairBoxes.DeleteBoxes(checkedItemID);
            }
            catch (Exception ex)
            {
                Filter = oldFilter;
                ErrorLog.WriteToFile("RepairBoxListCtrl.ascx.cs", "DeleteCheckedItems", ex.StackTrace);
                throw ex;
            }
        }

        public void DeleteSelectedItem()
        {
            throw new NotImplementedException();
        }

        public void AddNewItem(MyGeneration.dOOdads.SqlClientEntity newItem)
        {
            throw new NotImplementedException();
        }

        public void UpdateItem(MyGeneration.dOOdads.SqlClientEntity updatedItem)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}